package com.etc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.entity.Dept;
import com.etc.entity.Employees;
import com.etc.entity.LeaveApproval;
import com.etc.service.IDeptService;
import com.etc.service.lLeaveapprovallService;
import com.etc.service.impl.DeptServiceImpl;
import com.etc.service.impl.LeaveapprovallServicelmpl;
import com.google.gson.Gson;

/**
 * Servlet implementation class LeaveController
 */
@WebServlet("/LeaveController")
public class LeaveController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	LeaveapprovallServicelmpl service =new LeaveapprovallServicelmpl();
	IDeptService ds = new DeptServiceImpl();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LeaveController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String op=request.getParameter("op");
		HttpSession session = request.getSession();
		if("leave".equals(op)) {
			//提交请假条申请
			addleave(request,session,response);
		}else if("queryDept".equals(op)) {
			//查询所有部门
			queryDept(request,response);
		}else if("queryLeave".equals(op)) {
			//查询部门内所有请假条
			queryLeave(request,response);
		}else if("agreeLeave".equals(op)) {
			agreeLeave(request,response);
		}else if("refuseLeave".equals(op)) {
			refuseLeave(request,response);
		}else if("queryMyLeave".equals(op)) {
			queryMyLeave(request,response,session);
		}
		



	}
	
	//查看自己的请假条结果
	private void queryMyLeave(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		Employees emp = (Employees)session.getAttribute("emp");
		List<LeaveApproval> list = service.getMyLeaveById(emp.getEmpId());
		Gson gson = new Gson();
		String json = gson.toJson(list);
		System.out.println(json);
		out.print(json);
		out.close();
	}

	//拒批请假条
	private void refuseLeave(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int approvalId = Integer.parseInt(request.getParameter("approvalId"));
		int deptId = Integer.parseInt(request.getParameter("deptId"));
		PrintWriter out = response.getWriter();
		service.refuseLeave(approvalId);
		List<LeaveApproval> leaveList = service.getBeCorrectLeaveByDeptId(deptId);
		Gson gson = new Gson();
		String json = gson.toJson(leaveList);
		out.print(json);
		out.close();
		
	}

	//批准请假条
	private void agreeLeave(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int approvalId = Integer.parseInt(request.getParameter("approvalId"));
		int deptId = Integer.parseInt(request.getParameter("deptId"));
		PrintWriter out = response.getWriter();
		service.approvalLeave(approvalId);
		List<LeaveApproval> leaveList = service.getBeCorrectLeaveByDeptId(deptId);
		Gson gson = new Gson();
		String json = gson.toJson(leaveList);
		out.print(json);
		out.close();
	}

	//查询部门内所有请假条
	private void queryLeave(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		int deptId = Integer.parseInt(request.getParameter("deptId"));
		List<LeaveApproval> leaveList = service.getBeCorrectLeaveByDeptId(deptId);
		Gson gson = new Gson();
		String json = gson.toJson(leaveList);
		out.print(json);
		out.close();
	}

	//查询所有部门
	private void queryDept(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		List<Dept> list = ds.getqueryDept();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		out.print(json);
		out.close();
	}

	//提交请假条申请
	private void addleave(HttpServletRequest request, HttpSession session, HttpServletResponse response) throws IOException {
		Employees emp = (Employees)session.getAttribute("emp");
		String leaveTime=request.getParameter("leaveTime");
		String leaveReasons=request.getParameter("leaveReason");
		String backTime=request.getParameter("endTime");
		LeaveApproval leave=new LeaveApproval(emp,leaveReasons,leaveTime,backTime);
		boolean leave1=service.getaddLeave(leave);
		if (leave1) {
			PrintWriter pw=response.getWriter();    
			pw.write("<script>location.href='front/services.jsp'</script>");
		}else {
			PrintWriter pw=response.getWriter();    
			pw.write("<script>alert('请重新填写提交表单');location.href='front/leave.jsp'</script>");
		}
		
		
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
